from selenium import webdriver
import re
from data_parse import UrlFilter
from data_storage import TaobaoStorage
import threading

class TaobaoSpider(threading.Thread):
    def __init__(self,q):
        super().__init__()
        self.driver = webdriver.PhantomJS(executable_path=r'.\browser\phantomjs.exe')
        self.filter = UrlFilter()
        self.mongo = TaobaoStorage()
        self.queue = q

    def run(self):
        while True:
            link = self.queue.get()
            if link.startswith('https:'):
                pass
            else:
                link = 'https:'+link

            if link.startswith('https://item.taobao.com'):
                taobao_product = self.taobao_parse(link)
                if not self.filter.id_check(taobao_product['商品ID']):
                    self.mongo.insert_one(taobao_product)
            elif link.startswith('https://detail.tmall.com'):
                tmall_product = self.tamll_parse(link)
                if not self.filter.id_check(tmall_product['商品ID']):
                    self.mongo.insert_one(tmall_product)
            else:
                print('广告地址，直接抛弃。。。')

            if self.queue.empty():
                break

    def taobao_parse(self,url):
        '''
        淘宝商品
        :param url:
        :return:
        '''
        self.driver.get(url)
        self.driver.implicitly_wait(10)
        product_name = self.driver.find_elements_by_xpath("//h3[@class='tb-main-title']")[0].text
        price = self.driver.find_elements_by_xpath("//em[@id='J_PromoPriceNum']")[0].text
        product_id = re.findall(r'\d{8,15}',url)[0]
        product_name = ''.join(product_name.split())
        product = {'商品ID': product_id, '商品名称': product_name, '价格': price, '数据源':'淘宝' }
        return product

    def tamll_parse(self,url):
        '''
        天猫商品
        :param url:
        :return:
        '''
        self.driver.get(url)
        self.driver.implicitly_wait(10)
        product_id = re.findall(r'\d{8,15}', url)[0]
        try:
            product_name = self.driver.find_elements_by_xpath("//div[@class='tb-detail-hd']/h1")[0].text
        except IndexError:
            print('商品名称异常，链接：{}'.format(url))
            product_name = 'None'
        try:
            price = self.driver.find_elements_by_xpath("//dl[@id='J_StrPriceModBox']/dd/span[@class='tm-price']")[0].text
        except IndexError:
            price = self.driver.find_elements_by_xpath("//dl[@class='tm-tagPrice-panel']/dd/span[@class='tm-price']")[0].text#专柜价
        try:
            promo_price = self.driver.find_elements_by_xpath("//div[@class='tm-promo-price']/span[@class='tm-price']")[0].text
        except IndexError:
            promo_price = ""
        product_name = ''.join(product_name.split())
        product = {'商品ID':product_id,'商品名称':product_name,'价格':price,'促销价格':promo_price,'数据源':'天猫'}
        return product



